//
//  LRXRegisterController.m
//  路人侠
//
//  Created by zhangxulong on 3/5/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXRegisterController.h"
#import "zxlCircleView.h"
#import "LRXPhoneNumView.h"

#import "LRXPasswordView.h"

@interface LRXRegisterController()<
    LRXPhoneNumDelegate,
    LRXPasswordDelegate
>
@property (nonatomic,weak) UIView * titleView;
@property (nonatomic,weak) UIScrollView * contentView;
@property (nonatomic,weak) LRXPhoneNumView * phoneNumView;
@property (nonatomic,weak) LRXPasswordView * passwordView;
@property (nonatomic,strong) NSMutableArray * textField;
@property (nonatomic,copy) NSString * phoneNum;
@property (nonatomic,copy) NSString * authNum;
@property (nonatomic,weak) UIImageView * backImage;
@end
@implementation LRXRegisterController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willHide) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad{

    self.textField = [NSMutableArray array];
    self.view.backgroundColor = [UIColor colorWithRed:233 green:241 blue:253];
    UIImageView * backImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"regback1"]];
    backImage.frame = self.view.frame;
    self.backImage = backImage;
    
    [self.view addSubview:backImage];
    //标题布局
    self.navigationItem.title = @"注册";
    
    
    //界面布局
    [self contentViewLayout];
    
    //其他界面布局
    [self otherViewLayout];
    

}
- (void)otherViewLayout
{
    UILabel * reLogLabel = [[UILabel alloc]init];
    reLogLabel.text = @"已经有账号了？";
    reLogLabel.textColor = [UIColor whiteColor];
    reLogLabel.font = [UIFont systemFontOfSize:25 * hScale];
    CGFloat reLogLabelY = CGRectGetMaxY(self.contentView.frame) + 40 * hScale;
    reLogLabel.frame = CGRectMake(210 * wScale, reLogLabelY, 0, 0);
    [reLogLabel sizeToFit];
    
    UIButton * logBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [logBtn setTitle:@"登陆" forState:UIControlStateNormal];
    [logBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    logBtn.titleLabel.font = [UIFont systemFontOfSize:25 * hScale];
    logBtn.frame = CGRectMake(390 * wScale, reLogLabelY - (10 * hScale), 0, 0);
    [logBtn sizeToFit];
    [logBtn addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:reLogLabel];
    [self.view addSubview:logBtn];
}
//撤销模态窗口
- (void)dismissViewController
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
//内容窗口的布局
- (void)contentViewLayout
{
    //定义 scrollView 的大小位置
    UIScrollView * contentScrollView = [[UIScrollView alloc]init];
    contentScrollView.backgroundColor = [UIColor clearColor];
    contentScrollView.pagingEnabled = YES;
    contentScrollView.scrollEnabled = NO;
    self.contentView = contentScrollView;
    
    
    CGFloat contentScrollViewY = 420 * hScale;
    CGFloat contentScrollViewH = 315 * hScale;
    contentScrollView.frame = CGRectMake(0, contentScrollViewY, self.view.frame.size.width, contentScrollViewH);
    contentScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2 , contentScrollViewH);
    
    //创建子视图
    //填写手机号码界面
    LRXPhoneNumView * phoneNumView = [LRXPhoneNumView layoutPhoneNumSubView];
    phoneNumView.frame = CGRectMake(0, 0, self.view.frame.size.width, contentScrollViewH);
    phoneNumView.phoneDelegate = self;
    self.phoneNumView = phoneNumView;
    
    LRXPasswordView * passwordView = [LRXPasswordView layoutPasswordView];
    passwordView.frame = CGRectMake(self.view.frame.size.width * 1, 0, self.view.frame.size.width, contentScrollViewH);
    passwordView.passwordDelegate = self;
    self.passwordView = passwordView;
    
    [self.textField addObjectsFromArray:self.phoneNumView.textFiels];
    [self.textField addObjectsFromArray:self.passwordView.textFiels];
    
    
    contentScrollView.showsHorizontalScrollIndicator = NO;
    [contentScrollView addSubview:passwordView];
    [contentScrollView addSubview:phoneNumView];
    [self.view addSubview:contentScrollView];
    
    
}
//滚动View 的代理方法
- (void)changCurView:(NSInteger)page
{
    CATransition * transiton = [CATransition animation];
    transiton.type = kCATransitionFade;
    transiton.subtype =  kCATransitionFromRight;
    transiton.duration = 0.5;
    [self.backImage.layer addAnimation:transiton forKey:nil];
    self.backImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"regback%ld",page+1]];
    
    [UIView animateWithDuration:0.5 animations:^{

        self.passwordView.phoneNum = self.phoneNumView.phoneNum;
        self.contentView.contentOffset = CGPointMake(page * self.view.frame.size.width, 0);
    }];
}
- (void)willShow:(NSNotification *)noti
{
    UITextField * currentTf = self.textField[[self indexOfFirstResponder]];
    CGPoint point = CGPointMake(CGRectGetMinX(currentTf.frame), CGRectGetMaxY(currentTf.frame));
    
    CGFloat maxY = [currentTf convertPoint:point toView:self.view].y;
    
    //2.获取键盘的y值
    CGRect kbEndFrm = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat kbY = kbEndFrm.origin.y - 90 * hScale;
    
    //3.进行比较  计算距离
    CGFloat delta = kbY - maxY;
    //需要往上移
    if(delta < 0){
        [UIView animateWithDuration:0.25 animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(0, delta);
        }];
    }
}
/**
 * 恢复原状
 */
- (void)willHide
{
    [UIView animateWithDuration:0.25 animations:^{
        self.view.transform = CGAffineTransformIdentity;
    }];
}
/**
 *  获取当前的响应者的索引
 */
-(NSInteger)indexOfFirstResponder{
    
    //isFirstResponder 判断对象是否时第一响应者
    
    for (UITextField * tf in self.textField) {
        if (tf.isFirstResponder) {
            return tf.tag - 1110;
        }
    }
    //返回一1没有当前响应者
    return -1;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
